package saas.adapter.sdk.core.aspect;

import com.baozun.saas.core.context.TenantContext;
import com.baozun.scm.baseservice.message.common.MessageCommond;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import saas.adapter.sdk.core.context.InboundConversionTenantContext;

/**
 * @title: RmqConsumeAopAdvice
 * @description: rmq消费切面增强
 * @author: guan.xin
 * @create: 2021-11-17 14:09
 * @since: JDK8
 */
@Slf4j
@Aspect
@Component
public class RmqConsumeAopAdvice {

    @Pointcut("execution(public * saas..*.*(..)) && args(com.baozun.scm.baseservice.message.common.MessageCommond)")
    public void consume() {
    }

    @Around("consume()")
    public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        MessageCommond mc = (MessageCommond) proceedingJoinPoint.getArgs()[0];
        String topic = mc.getTopic();

        String saasTenantCode = TenantContext.getCurrentSaaSTenantCode();
        if (Strings.isNullOrEmpty(saasTenantCode)) {
            log.warn("消息主题：{}，saasTenantCode为空", topic);
        } else {
            // 设置映射后租户上下文
            InboundConversionTenantContext.setTenantContextByMpSaasTenantCode(saasTenantCode, true);
        }
        // 执行方法
        proceedingJoinPoint.proceed();
    }


}
